# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer:
pkgname=ocaml-lwt
_pkgname=lwt
pkgver=5.6.1
pkgrel=0
pkgdesc="OCaml promises and concurrent I/O"
url="https://github.com/ocsigen/lwt"
arch="all !riscv64"  # limited by ocaml aport
license="MIT"
depends="
	ocaml-runtime
	ocaml-mmap
	ocaml-ocplib-endian
	ocaml-result
	"
depends_dev="
	libev-dev
	ocaml-mmap-dev
	ocaml-ocplib-endian-dev
	ocaml-ppxlib-dev
	ocaml-react-dev
	ocaml-result-dev
	"
makedepends="$depends_dev dune dune-configurator ocaml ocaml-cppo"
options="!check"  # FIXME: tests hang
subpackages="$pkgname-dev ${pkgname}_react:_react ${pkgname}_ppx:_ppx"
source="$pkgname-$pkgver.tar.gz::https://github.com/ocsigen/$_pkgname/archive/$pkgver.tar.gz"
builddir="$srcdir/$_pkgname-$pkgver"
_ocamldir=usr/lib/ocaml

# 32-bit archs
case "$CARCH" in
	arm*|x86) options="$options textrels" ;;
esac

build() {
	dune exec \
		-p lwt,lwt_react,lwt_ppx \
		"src/unix/config/discover.exe" -- \
		--save --use-libev true

	dune build \
		-p lwt,lwt_react,lwt_ppx \
		--verbose
}

check() {
	dune runtest --verbose
}

package() {
	dune install \
		--destdir="$pkgdir" \
		--docdir=/.omit \
		lwt lwt_react lwt_ppx
	rm -Rf "$pkgdir"/.omit
}

dev() {
	default_dev
	depends="$depends_dev
		$pkgname=$pkgver-r$pkgrel
		${pkgname}_react=$pkgver-r$pkgrel
		${pkgname}_ppx=$pkgver-r$pkgrel"
	provides="${pkgname}_ppx-dev ${pkgname}_react-dev"

	cd "$pkgdir"

	local path; for path in $(find usr/lib/ocaml \( \
			-name '*.cmt' -o \
			-name '*.cmti' -o \
			-name '*.cmx' -o \
			-name '*.cmxa' -o \
			-name '*.ml' -o \
			-name '*.mli' \
		\))
	do
		amove "$path"
	done
}

_ppx() {
	pkgdesc="$pkgdesc (PPX syntax)"
	depends="$pkgname=$pkgver-r$pkgrel ocaml-runtime ocaml-ppxlib"

	amove usr/lib/ocaml/lwt_ppx
}

_react() {
	pkgdesc="$pkgdesc (for FRP)"
	depends="$pkgname=$pkgver-r$pkgrel ocaml-runtime ocaml-react"

	amove usr/lib/ocaml/lwt_react
}

sha512sums="
698875bd3bfcd5baa47eb48e412f442d289f9972421321541860ebe110b9af1949c3fbc253768495726ec547fe4ba25483cd97ff39bc668496fba95b2ed9edd8  ocaml-lwt-5.6.1.tar.gz
"
